<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="queryParams.password"
          placeholder="请输入密码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="角色ID" prop="roleId">
        <el-input
          v-model="queryParams.roleId"
          placeholder="请输入角色ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createtime">
        <el-date-picker clearable
          v-model="queryParams.createtime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="updatetime">
        <el-date-picker clearable
          v-model="queryParams.updatetime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['tbuser:tbuser:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tbuser:tbuser:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tbuser:tbuser:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['tbuser:tbuser:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tbuserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户ID" align="center" key="uid" prop="uid"
      v-if="columns[0].visible" />
      <el-table-column label="用户名" align="center" key="username" prop="username" v-if="columns[1].visible"/>
      <el-table-column label="性别" align="center" prop="gender" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="角色" align="center" prop="roleName" />
      <el-table-column label="状态" align="center" key="status">
        <template #default="scope">
          <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createtime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatetime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updatetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['tbuser:tbuser:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['tbuser:tbuser:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="tbuserRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入昵称" />
        </el-form-item>
        <!-- 下拉框选项男/女 -->
        <el-form-item label="性别" prop="gender">
            <el-select v-model="form.gender" placeholder="请选择性别">
              <el-option label="男" value="男"></el-option>
              <el-option label="女" value="女"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="角色" prop="roleId">
          <el-select v-model="form.roleId" placeholder="请选择身份">
            <el-option v-for="item in roleList" :key="item.roleId" :label="item.roleName" :value="item.roleId"></el-option>
          </el-select>
            
        </el-form-item>
        <!-- 状态从字典获取 -->
         <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
                <el-radio v-for="dict in sys_normal_disable" :key="dict.value" :value="dict.value">{{ dict.label }}</el-radio>
              </el-radio-group>
         </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Tbuser">
import { listTbuser, getTbuser, delTbuser, addTbuser, updateTbuser } from "@/api/tbuser/tbuser"
import {listTbrole} from "@/api/tbrole/tbrole"
import { el, fa } from "element-plus/es/locales.mjs"
import{ changeUserStatus} from "@/api/tbuser/tbuser"

const { proxy } = getCurrentInstance()
const { sys_normal_disable } = proxy.useDict("sys_normal_disable")

const tbuserList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const roleList = ref([])


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    uid: null,
    username: null,
    password: null,
    gender: null,
    phone: null,
    nickName: null,
    roleId: null,
    status: null,
    createtime: null,
    updatetime: null,
  },
  rules: {
    username: [
      { required: true, message: "用户名不能为空", trigger: "blur" }
    ],
    password: [
      { required: true, message: "密码不能为空", trigger: "blur" }
    ],
    gender: [
      { required: true, message: "性别不能为空", trigger: "blur" }
    ],
    roleId: [
      { required: true, message: "身份不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ],
  }
})
const columns = ref([
  { key: 0, label: `用户ID`, visible: true },
  { key: 1, label: `用户名`, visible: true },
  { key: 2, label: `性别`, visible: true },
  { key: 3, label: `手机号`, visible: true },
  { key: 4, label: `昵称`, visible: true },
  { key: 5, label: `角色`, visible: true },
  { key: 6, label: `状态`, visible: true }
])
const { queryParams, form, rules } = toRefs(data)
function getRoleList() {
  listTbrole(queryParams.value).then(response => {
    loading.value = true

    roleList.value = response.rows
    total.value = res.total
  })
}
getRoleList()
/** 查询用户管理列表 */
function getList() {
  listTbuser(queryParams.value).then(response => {
    loading.value = false
    tbuserList.value = response.rows
    total.value = response.total
    
  })
}


// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    uid: null,
    username: null,
    password: null,
    gender: null,
    phone: null,
    nickName: null,
    roleId: null,
    status: null,
    createtime: null,
    updatetime: null
  }
  proxy.resetForm("tbuserRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.uid)
  single.value = selection.length != 1
  multiple.value = !selection.length
}
/** 用户状态修改  */
// function handleStatusChange(row) {
//   loading.value = false
//   const text = row.status === "0" ? "启用" : "停用"
//   proxy.$modal.confirm('确认要"' + text + '""' + row.uid + '"用户吗?').then(function () {
//     return changeUserStatus(row.uid, row.status)
//   }).then(() => {
//     proxy.$modal.msgSuccess(text + "成功")
//   }).catch(function () {
//     row.status = row.status === "0" ? "1" : "0"
//   })
// }

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加用户管理"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _uid = row.uid || ids.value
  getTbuser(_uid).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改用户管理"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["tbuserRef"].validate(valid => {
    if (valid) {
      if (form.value.uid != null) {
        updateTbuser(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addTbuser(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _uids = row.uid || ids.value
  proxy.$modal.confirm('是否确认删除用户管理编号为"' + _uids + '"的数据项？如若删除则无法恢复').then(function() {
    return delTbuser(_uids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('tbuser/tbuser/export', {
    ...queryParams.value
  }, `tbuser_${new Date().getTime()}.xlsx`)
}

getList()
</script>
